﻿using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shumisen.Core.Composition.Aop;

namespace Shumisen.Core.UnitTest.Composition.Aop
{
	[TestClass]
	public class InterceptorChainTest
	{
		[TestMethod]
		public void TestIntercept()
		{
			var builder = new StringBuilder();
			var chain = new InterceptorChain(
				new List<IInterceptor>
				{
					new MyInterceptor("1", builder),
					new MyInterceptor("2", builder),
					new MyInterceptor("3", builder),
					new MyInterceptor("4", builder),
					new MyInterceptor("5", builder),
				});
			chain.Intercept(new MyInvocation("6", builder));
			Assert.AreEqual("123456", builder.ToString());
		}

		public class MyInterceptor : IInterceptor
		{
			private readonly string _key;

			private readonly StringBuilder _builder;

			public MyInterceptor(String key, StringBuilder builder)
			{
				_key = key;
				_builder = builder;
			}

			public void Intercept(IInvocation invocation)
			{
				_builder.Append(_key);
				invocation.Proceed();
			}
		}

		public class MyInvocation : IInvocation
		{
			private readonly string _key;

			private readonly StringBuilder _builder;

			public MyInvocation(String key, StringBuilder builder)
			{
				_key = key;
				_builder = builder;
			}

			public Type TargetType
			{
				get
				{
					return null;
				}
			}

			public object TargetInstance
			{
				get
				{
					return null;
				}
			}

			public IMethodCallMessage MethodCallMessage
			{
				get
				{
					return null;
				}
			}

			public IMethodReturnMessage MethodReturnMessage
			{
				get
				{
					return null;
				}
			}

			public void Proceed()
			{
				_builder.Append(_key);
			}
		}
	}
}